<template>
	<view class="login">
		<u-navbar leftClick="leftClick" :autoBack="true">
		</u-navbar>


		<view class="" style="height: 182rpx;"></view>

		<view class="coverBox">
			<image class="coverImg" src="http://youcaius.top/uploads/image/cover.png"></image>
		</view>

		<view class="agreement">
			<view class="checked">
				<u-checkbox v-model="checked" shape="circle" activeColor="#F8AD22"
					@change="handleprotocol"></u-checkbox>
			</view>
			<view class="">
				已阅读并同意 <text style="color:#F8AD22 ;" @click="handleFu">《服务协议》</text>
			</view>

		</view>

		<view class="loginBtn" @click="$u.debounce(handleLogin, 500)">
			登录
		</view>
		<view class="youke" @click="handleYkLogin">
			游客登录
		</view>
		<!-- 	<button class="loginBtn" open-type="chooseAvatar"  @chooseavatar="onChooseAvatar" >  
			授权登录
		</button> -->

	</view>
</template>

<script>
	import {
		login,
		nameAvatat
	} from "@/api/api.js"
	const defaultAvatarUrl =
		'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

	export default {
		data() {
			return {
				checked: false,
				logincode: "",
				avatar: ""
			}
		},
		onLoad() {
			if (uni.getStorageSync("token")) {
				uni.switchTab({
					url: "../home/index"
				})
			}
		},
		onShow() {
			if (uni.getStorageSync("token")) {
				uni.switchTab({
					url: "../home/index"
				})
			}
		},
		methods: {
			handleYkLogin() {
				uni.switchTab({
					url: "../home/index"
				})
			},
			// 获取头像临时路径
			onChooseAvatar(e) {
				console.log(e);
				this.avatar = e.detail.avatarUrl


				uni.uploadFile({
					url: 'https://120.76.245.175/api/index/upload',
					filePath: this.avatar,
					name: 'file',
					formData: {
						'user': 'test'
					},
					success: (uploadFileRes) => {
						console.log("成功回调", uploadFileRes);
					}
				});

				uni.setStorageSync("avatar", this.avatar)
				if (this.checked == false) {
					uni.showToast({
						title: '您未同意服务协议',
						icon: "none",
						duration: 2000
					});
					return
				}
				this.handleLogin()
			},


			leftClick() {},
			handleprotocol() {
				this.checked = !this.checked
			},
			handleLogin() {
				let that = this
				if (that.checked == false) {
					uni.showToast({
						title: '您未同意服务协议!',
						icon: "none",
						duration: 2000
					});
					return
				}

				uni.login({
					provider: 'weixin',
					success: async(loginRes) => {
						console.log("微信获取code码", loginRes);
						that.logincode = loginRes.code

						let res = await login({
							type: "2",
							code: that.logincode,
						})
						if (res.code == 1000) {
							console.log("登录", res)
							uni.setStorageSync("token", res.data.token)
							uni.showToast({
								title: res.message,
								icon: "success",
								duration: 1000
							});
							setTimeout(() => {
								uni.switchTab({
									url: "../home/index"
								})
							}, 1500)
						}
						// uni.getUserInfo({
						// 	provider: 'weixin',
						// 	success: async function(infoRes) {
						// 		console.log(infoRes.userInfo)
						// 		console.log('用户昵称为：' + infoRes.userInfo.nickName);
						// 		let res = await login({
						// 			type: "2",
						// 			code: that.logincode,
						// 		})
						// 		if (res.code == 1000) {
						// 			console.log("登录", res)
						// 			uni.setStorageSync("token", res.data.token)
						// 			uni.showToast({
						// 				title: res.message,
						// 				icon: "success",
						// 				duration: 1000
						// 			});
						// 			setTimeout(() => {
						// 				uni.switchTab({
						// 					url: "../home/index"
						// 				})
						// 			}, 1500)
						// 		}

						// 	},
						// 	fail(e) {
						// 		console.log(e,"444444")
						// 	}
						// });

					}
				})


			},
			handleFu() {
				uni.navigateTo({
					url: "../mine/serviceAgreement"
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.login {
		width: 100%;
		min-height: 100%;
		background-color: #ffffff;

		.youke {
			width: 100%;
			text-align: center;
			color: #F8AD22;
			font-size: 28rpx;
		}

		.coverBox {
			width: 650rpx;
			height: 900rpx;
			margin: 0 auto;
			margin-top: 30px;

			.coverImg {
				width: 100%;
				height: 100%;
			}
		}

		.agreement {
			font-size: 28rpx;
			font-family: PingFang SC-Regular, PingFang SC;
			font-weight: 400;
			color: #666666;
			line-height: 33rpx;
			display: flex;
			align-items: center;
			width: 640rpx;
			margin: 0 auto;
			margin-top: 100rpx;
		}

		.loginBtn {
			width: 640rpx;
			height: 88rpx;
			background: #F8AD22;
			border-radius: 20rpx 20rpx 20rpx 20rpx;
			opacity: 1;
			text-align: center;
			line-height: 88rpx;
			color: #ffffff;
			margin: 25rpx auto;
			outline: none;
			border: #F8AD22 1px solid;

		}
	}
</style>